//
//  Copyright (C) 1998-2007 J. Andrew McLaughlin
//  Copyright (C) 2009 Vadim Ushakov
// 
//  This program is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by the Free
//  Software Foundation; either version 2 of the License, or (at your option)
//  any later version.
// 
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
//  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//  for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with this program; if not, write to the Free Software Foundation, Inc.,
//  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

/****************************************************************************/

int kernelWindowSetBackgroundImage(kernelWindow *window, image *imageCopy)
{
	// Set the window's background image

	int status = 0;

	// Make sure we've been initialized
	if (!initialized)
		return (status = ERR_NOTINITIALIZED);

	// Check params
	if ((window == NULL) || (imageCopy == NULL))
		return (status = ERR_NULLPARAMETER);
  
	// If there was a previous background image, deallocate its memory
	if (window->backgroundImage.data)
	{
		kernelFree(window->backgroundImage.data);
		window->backgroundImage.data = NULL;
	}

	// Copy the image information into the window's background image
	kernelMemCopy(imageCopy, (void *) &(window->backgroundImage), sizeof(image));

	// Get some new memory for the image data
	window->backgroundImage.data = kernelMalloc(window->backgroundImage.dataLength);
	if (window->backgroundImage.data == NULL)
		return (status == ERR_MEMORY);
  
	// Copy the image data into new memory
	kernelMemCopy(imageCopy->data, window->backgroundImage.data, window->backgroundImage.dataLength);

	return (status = 0);
}

/****************************************************************************/

int kernelWindowLoadBackgroundImageFromFile(kernelWindow *window, const char *filename, backgroundImageMode mode)
{
	int status = 0;
	image backgroundImage;

	if (!initialized)
		return (status = ERR_NOTINITIALIZED);

	if ((filename == NULL) || (window == NULL))
		return (status = ERR_NULLPARAMETER);
  
	// Try to load the new background image
	status = kernelImageLoad(filename, 0, 0, &backgroundImage);
	if (status < 0)
	{
		kernelError(kernel_error, "Error loading background image");
		return (status);
	}

	// Set background image and mode
	kernelWindowSetBackgroundImage(window, &backgroundImage);
	kernelWindowSetBackgroundImageMode(window, mode);

	// Release the image memory
	kernelMemoryRelease(backgroundImage.data);

	// Redraw the root window
	kernelWindowDraw(rootWindow);

	return (status = 0);
}

/****************************************************************************/

int kernelWindowSetBackgroundImageMode(kernelWindow *window, backgroundImageMode mode)
{
	int status = 0;

	if (!initialized)
		return (status = ERR_NOTINITIALIZED);

	if (window == NULL)
		return (status = ERR_NULLPARAMETER);

	status = window->backgroundImageMode;
	if (mode != backgroundImageNop)
		window->backgroundImageMode = mode;

	return (status);
}

/****************************************************************************/

int kernelWindowGetBackgroundImageMode(kernelWindow *window)
{
	int status = 0;

	if (!initialized)
		return (status = ERR_NOTINITIALIZED);

	if (window == NULL)
		return (status = ERR_NULLPARAMETER);

	status = window->backgroundImageMode;

	return (status);
}

/****************************************************************************/

int kernelWindowRootLoadBackgroundImageFromFile(const char *filename, backgroundImageMode mode)
{
	// This will tile the supplied image as the background image of the
	// root window

	int status = 0;
	//variableList settings;

	if (!initialized)
		return (status = ERR_NOTINITIALIZED);

	if (rootWindow == NULL)
		return (status = ERR_NULLPARAMETER);

	status = kernelWindowLoadBackgroundImageFromFile(rootWindow, filename, mode);
	if (status < 0)
		return (status);
/*
	if (saveInConfig)
	{
		status = kernelConfigRead(WINDOW_DEFAULT_DESKTOP_CONFIG, &settings);
		if (status >= 0)
		{
			char modeString[128];
			sprintf(modeString, "%i", (int)kernelWindowGetBackgroundImageMode(rootWindow));
			kernelVariableListSet(&settings, "background.image", filename);
			kernelVariableListSet(&settings, "background.imageMode", modeString);
			kernelDisk *configDisk = kernelDiskGetByPath(WINDOW_DEFAULT_DESKTOP_CONFIG);
			if (configDisk && !configDisk->filesystem.readOnly)
				kernelConfigWrite(WINDOW_DEFAULT_DESKTOP_CONFIG, &settings);
			kernelVariableListDestroy(&settings);
		}
	}
*/
	return (status = 0);
}

/****************************************************************************/

